package com.guli.gulimall.seckill.controller;

import com.guli.common.utils.R;
import com.guli.gulimall.seckill.service.SeckillService;
import com.guli.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀的商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SecKillSkuRedisTo> redisTos = seckillService.getCurrentSeckillSkus();
        return R.ok().setDate(redisTos);
    }

    /**
     * 查询该商品是否参与秒杀
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SecKillSkuRedisTo redisTo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setDate(redisTo);
    }

    //  /killId?=7-6&key=63979b92a8d545db9f51eb3b3e8e4f5b&num=1

    /**
     * 秒杀服务
     *
     * @param killId 场次id_商品id
     * @param key    随机码
     * @param num    件数
     * @return
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
        String orderSn = seckillService.seckill(killId, key, num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
